package com.example.todolist.view

import android.annotation.SuppressLint
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.todolist.commen.TodoCategoryEnum
import com.example.todolist.database.model.CategoryEntity
import com.example.todolist.databinding.CategoryListItemBinding
import com.example.todolist.entity.CategoryDetail
import com.example.todolist.util.LoggerTag

class CategoryfragAdapter(
    private val exitDelMode: () -> Unit, private val startDelMode: () -> Unit
) : ListAdapter<CategoryDetail, CategoryfragAdapter.detailViewHolder>(TodoDiffCallback()) {
    private var isDelMode = false
    private var sections = ArrayList<CategoryEntity>()
    override fun onCreateViewHolder(
        parent: ViewGroup, viewType: Int
    ): detailViewHolder {
        val binding =
            CategoryListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val viewHolder = detailViewHolder(binding)
        return viewHolder

    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onBindViewHolder(
        holder: detailViewHolder, position: Int
    ) {
        val item = getItem(position)
        holder.bind(item)
        // 启动删除模式
        holder.container.setOnLongClickListener { v ->
            startAdapterDelMode()
            true
        }
        // 点击删除item
        holder.container.setOnClickListener {
            changeSections(item, holder)
        }
        holder.checkbox.setOnClickListener {
            changeSections(item, holder)

        }
    }

    // 批量删除items的操作
    private fun changeSections(
        item: CategoryDetail, holder: detailViewHolder
    ) {
        if (isDelMode) {
            item.isSelected = !item.isSelected
            holder.checkbox.isChecked = item.isSelected
            Log.d(LoggerTag.MAIN_CATEGORY, "onBindViewHolder: ${item.categoryId}")
            if (item.isSelected) {
                sections.add(CategoryEntity(categoryId = item.categoryId))
                Log.d(LoggerTag.MAIN_CATEGORY, "onBindViewHolder: ${sections}")
            } else {
                //                    val iterator = sections.iterator()
                //                    while (iterator.hasNext()) {
                //                        val entity = iterator.next()
                //                        if (entity.categoryId == item.categoryId) {
                //                            iterator.remove()
                //                        }
                //                    }
                sections.removeIf { it.categoryId == item.categoryId }
                Log.d(LoggerTag.MAIN_CATEGORY, "onBindViewHolder: ${sections}")
            }
        }
    }

    inner class detailViewHolder(val binding: CategoryListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        val tvName = binding.categoryName
        val tvCnt = binding.count
        val checkbox = binding.delBox
        val container = binding.cgItem
        fun bind(cg: CategoryDetail) {
            tvName.text = cg.categoryName
            tvCnt.text = cg.categoryCnt.toString()
            checkbox.visibility = View.GONE
            if (isDelMode) {
                if (cg.categoryId != TodoCategoryEnum.UNCATEGORY.code && cg.categoryId != TodoCategoryEnum.CATEGORY_ALL.code)
                    checkbox.visibility = View.VISIBLE
                checkbox.isChecked = cg.isSelected
            }
        }
    }

    // 用于比较新旧数据的 DiffUtil 回调
    class TodoDiffCallback : DiffUtil.ItemCallback<CategoryDetail>() {
        override fun areItemsTheSame(oldItem: CategoryDetail, newItem: CategoryDetail): Boolean {
            return oldItem.categoryId == newItem.categoryId // 比较唯一标识
        }

        @SuppressLint("DiffUtilEquals")
        override fun areContentsTheSame(oldItem: CategoryDetail, newItem: CategoryDetail): Boolean {
            return oldItem.equals(newItem) // 比较内容是否相同
        }
    }


    fun exitAdapterDelMode() {
        isDelMode = false
        notifyItemRangeChanged(0, itemCount) // 刷新所有项
    }

    private fun startAdapterDelMode() {
        isDelMode = true
        startDelMode()
        notifyItemRangeChanged(0, itemCount) // 刷新所有项
    }

    fun getSelectedItems() = sections
    fun initSelectedItems() {
//        submitList(currentList)
        sections.clear()
    }
}

